package com.zhao.uitest.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhao.uitest.R;
import com.zhao.uitest.listview.Fruit;

import java.util.List;

/**
 * Created by Zhao on 2016/2/21.
 * 自定义适配器
 */
public class FruitAdpter extends ArrayAdapter<Fruit> {
    private int resourceId;


    public FruitAdpter(Context context, int resource, List<Fruit> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Fruit fruit = getItem(position); // 获取当前项的Fruit实例
        View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
        ImageView fruitImage = (ImageView) view.findViewById(R.id.iv_fruit_photo);
        TextView fruitName = (TextView) view.findViewById(R.id.tv_fruit_name);
        fruitImage.setImageResource(fruit.getImageId());
        fruitName.setText(fruit.getName());
        return view;
    }
}
